import { app, Tray, Menu, nativeImage, BrowserWindow } from 'electron'
import path from 'path'
import { toggleWin } from './win'

const iconPath = path.join(__dirname, './images/icon.png')

export const createTray = (win: BrowserWindow, path: string = iconPath) => {
  const icon = nativeImage.createFromPath(path)
  const tray = new Tray(icon)
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '显示/隐藏',
      click: () => {
        toggleWin(win)
      }
    },
    {
      type: 'separator'
    },
    {
      label: '重启',
      click: () => {
        app.relaunch()
        app.quit()
      }
    },
    {
      label: '退出',
      role: 'quit'
    }
  ])

  tray.setContextMenu(contextMenu)
  tray.setToolTip('托盘悬浮提示')

  // 点击托盘显示窗口
  tray.on('click', () => {
    toggleWin(win)
  })
}
